
clear all; close all;

try

    Screen('Preference', 'VisualDebuglevel', 3);

screenNum=0;
[wPtr,rect]=Screen('OpenWindow',screenNum,0,[0 0 800 600]);


HideCursor;
white=WhiteIndex(wPtr);
black=BlackIndex(wPtr);

Screen('FillRect',wPtr,white);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5



tGuess=25;  % umbral estimado 
	tGuessSd=5; % sd estimada 
    
    %%%parametros para el algoritmo quest
    
    pThreshold=0.82;
beta=3.50e1;delta=0.01;gamma=0.5;
q=QuestCreate(tGuess,tGuessSd,pThreshold,beta,delta,gamma);
q.normalizePdf=1; % This adds a few ms per call to QuestUpdate, but otherwise the pdf will underflow after about 1000 trials.

% On each trial we ask Quest to recommend an intensity and we call QuestUpdate to save the result in q.
trialsDesired=10;

for k=1:trialsDesired
	tTest=QuestQuantile(q);	% Recommended by Pelli (1987), and still our favorite.
	
	% We are free to test any intensity we like, not necessarily what Quest suggested.
	% 	tTest=min(-0.05,max(-3,tTest)); % Restrict to range of log contrasts that our equipment can produce.
	
	% Simulate a trial

    fix(tTest)
showimage('o',fix(tTest),wPtr,1);  % muestra el hombre 2 por 5 segundos


Screen('FillRect',wPtr,black);
Screen('TextSize', wPtr , 20);
Screen('DrawText', wPtr, 'hombre(0) o mujer(1):', 200, 200, [255 50 255]);
Screen(wPtr, 'Flip');


response=[2];
while response~=1 & response~=0
    response=input('hombre(0) o mujer(1): '); 
end



% 
% 
%   response=QuestSimulate(q,tTest,tActual);
%  	
% 	% Update the pdf
 	q=QuestUpdate(q,tTest,response); % Add the new datum (actual test intensity and observer response) to the database.
end


% Ask Quest for the final estimate of threshold.
t=QuestMean(q);		% Recommended by Pelli (1989) and King-Smith et al. (1994). Still our favorite.
sd=QuestSd(q);
fprintf('Final threshold estimate (mean?sd) is %.2f ? %.2f\n',t,sd);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%55


Screen('CloseAll');
ShowCursor;


catch
    
Screen('CloseAll');
ShowCursor;

rethrow(lasterror)

end

